package com.fqgame.auth.controller;

import com.fqgame.auth.dto.LoginRequest;
import com.fqgame.auth.dto.LoginResponse;
import com.fqgame.auth.service.UserService;
import com.fqgame.common.response.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 认证控制器
 */
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final UserService userService;

    /**
     * 登录
     *
     * @param loginRequest 登录请求
     * @return 登录响应
     */
    @PostMapping("/login")
    public Result<LoginResponse> login(@RequestBody @Valid LoginRequest loginRequest) {
        LoginResponse loginResponse = userService.login(loginRequest);
        return Result.success(loginResponse);
    }

    /**
     * 刷新令牌
     *
     * @param token 原令牌
     * @return 新令牌
     */
    @GetMapping("/refresh")
    public Result<String> refreshToken(@RequestParam String token) {
        String newToken = userService.refreshToken(token);
        return Result.success(newToken);
    }
}